<style lang="less">
    @import "main.less";
</style>
<template>
    <div class="main" :class="{'main-hide-text': shrink}">
        <div class="sidebar-menu-con" :style="{width: shrink?'60px':'200px', overflow: shrink ? 'visible' : 'auto'}">
            <shrinkable-menu
                    :shrink="shrink"
                    :theme="menuTheme"
                    :before-push="beforePush"
                    :open-names="openedSubmenuArr"
                    :menu-list="menuList">
                <div slot="top" class="logo-con">
                    <img v-show="!shrink"  src="../images/logo.png" key="max-logo" />
                    <img v-show="shrink" src="../images/logo.png" key="min-logo" />
                </div>
            </shrinkable-menu>
        </div>
        <div class="main-header-con" :style="{paddingLeft: shrink?'60px':'200px'}" style="min-width: 800px;">
            <div class="main-header">
                <!-- 收缩 -->
                <div class="navicon-con">
                    <Button :style="{transform: 'rotateZ(' + (this.shrink ? '-90' : '0') + 'deg)'}" type="text" @click="toggleClick">
                        <Icon type="navicon" size="32"></Icon>
                    </Button>
                </div>
                <!-- 路径 -->
                <div class="header-middle-con">
                    <div class="main-breadcrumb">
                        <breadcrumb-nav :currentPath="currentPath"></breadcrumb-nav>
                    </div>
                </div>
                <!-- heard右侧信息 -->
                <div class="header-avator-con">
                    <full-screen v-model="isFullScreen" @on-change="fullscreenChange"></full-screen>
                    <message-tip v-model="mesCount"></message-tip>
                    <div class="user-dropdown-menu-con">
                        <Row type="flex" justify="end" align="middle" class="user-dropdown-innercon">
                            <Dropdown transfer trigger="click" @on-click="handleClickUserDropdown">
                                <a href="javascript:void(0)">
                                    <span class="main-user-name">{{ userName }}</span>
                                    <Icon type="arrow-down-b"></Icon>
                                </a>
                                <DropdownMenu slot="list">
                                    <DropdownItem name="ownSpace">个人中心</DropdownItem>
                                    <DropdownItem name="loginout" divided>退出登录</DropdownItem>
                                </DropdownMenu>
                            </Dropdown>
                            <Avatar :src="avatorPath" style="background: #619fe7;margin-left: 10px;"></Avatar>
                        </Row>
                    </div>
                </div>
            </div>
            <!-- tab -->
            <div class="tags-con">
                <tags-page-opened :pageTagsList="pageTagsList"></tags-page-opened>
            </div>
        </div>

        <div class="single-page-con" :style="{left: shrink?'60px':'200px'}">
            <div class="single-page">
                <keep-alive :include="cachePage">
                    <transition name="fade">
                        <router-view></router-view>
                    </transition>
                </keep-alive>
            </div>
        </div>
    </div>
</template>
<script>
    import Vue from 'vue';
    import shrinkableMenu from './main-components/shrinkable-menu/shrinkable-menu.vue';
    import breadcrumbNav from './main-components/breadcrumb-nav.vue';
    import tagsPageOpened from './main-components/tags-page-opened.vue';
    import messageTip from './main-components/message-tip.vue';
    import fullScreen from './main-components/fullscreen.vue';

    import appRouter from '../router/appRouter';
    import Main from '@/views/Main.vue';
    import Util from '@/libs/util';
    import Cookies from 'js-cookie';

    //请求用户权限
    const requestPermission = params => {
        return Util.axios.post('/ms/getPermission', {}, {withCredentials: true});
    };

    export default {
        components:{
            shrinkableMenu,
            breadcrumbNav,
            tagsPageOpened,
            fullScreen,
            messageTip
        },
        data() {
            return {
                shrink: false,
                userName: '',
                isFullScreen: false,
            };
        },
        computed: {
            openedSubmenuArr(){
                return this.$store.state.app.openedSubmenuArr;
            },
            menuList () {
                return this.$store.state.app.menuList;
            },
            pageTagsList () {
                return this.$store.state.app.pageOpenedList; // 打开的页面的页面对象
            },
            currentPath () {
                return this.$store.state.app.currentPath; // 当前面包屑数组
            },
            avatorPath () {
                return localStorage.avatorImgPath;
            },
            cachePage () {
                return this.$store.state.app.cachePage;
            },
            lang () {
                return null;
            },
            menuTheme () {
                return this.$store.state.app.menuTheme;
            },
            mesCount () {
                return null;
            }
        },
        methods: {
            handleStart() {
                this.$Modal.info({
                    title: 'Bravo',
                    content: 'Now, enjoy the convenience of iView.'
                });
            },
            init: function()
            {
                this.userName = Cookies.get('user');
            },
            toggleClick () {
                this.shrink = !this.shrink;
            },
            checkTag (name) {
                let openpageHasTag = this.pageTagsList.some(item => {
                    if (item.name === name) {
                        return true;
                    }
                });
                if (!openpageHasTag) { //  解决关闭当前标签后再点击回退按钮会退到当前页时没有标签的问题
                    Util.openNewPage(this, name, this.$route.params || {}, this.$route.query || {});
                }
            },
            handleClickUserDropdown (name) {
                if (name === 'ownSpace') {
                    Util.openNewPage(this, 'ownspace_index');
                    this.$router.push({
                        name: 'ownspace_index'
                    });
                } else if (name === 'loginout') {
                    // 退出登录
                    //this.$store.commit('logout', this);

                    //清除当前用户
                    Cookies.remove('user');
                    this.$store.commit('clearOpenedSubmenu');

                    Util.axios.post('/logout', {});

                    this.$router.push({
                        name: 'login'
                    });
                }
            },
            fullscreenChange (isFullScreen) {
                // console.log(isFullScreen);
            }
        },
        mounted () {
            this.init();
        },
        created: function () {
            // 显示打开的页面的列表
            this.$store.commit('setOpenedList');
        },
        watch: {
            '$route' (to) {
                this.$store.commit('setCurrentPageName', to.name);
                let pathArr = Util.setCurrentPath(this, to);
                if (pathArr.length > 1) {
                    this.$store.commit('addOpenSubmenu',  pathArr[1].name);
                }
                this.checkTag(to.name);
                localStorage.currentPageName = to.name;
            },
        }
//        beforeRouteEnter()
//        {
//            debugger;
//            let lastPath = sessionStorage.getItem("lastPath");
////            this.$router.push({path: lastPath});
//            next();
//        }

    }
</script>
